package com.pzl.hwr.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.pzl.hwr.R;

import java.util.ArrayList;

/**
 * Created by zl.peng on 2017/6/19 14:48.
 */

public class WordsAdapter extends RecyclerView.Adapter<WordsAdapter.VH> {

    private ArrayList<String> words;

    private Context context;

    private LayoutInflater inflater;

    private Listener listener;

    private int unit;


    public WordsAdapter(ArrayList<String> words, Context context, Listener listener, int unit) {
        this.words = words;
        this.context = context;
        this.listener = listener;
        this.unit = unit;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_words, null);
        return new VH(itemView);
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        final String word = words.get(position);
        holder.tvWords.setText(word);
        holder.tvWords.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) listener.onWordClick(word);
            }
        });

        holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(unit, unit));
    }

    @Override
    public int getItemCount() {
        return words == null ? 0 : words.size();
    }

    public void setData(ArrayList<String> words) {
        this.words = words;
    }

    class VH extends RecyclerView.ViewHolder {

        TextView tvWords;

        View itemView;

        public VH(View itemView) {
            super(itemView);
            this.itemView = itemView;
            tvWords = (TextView) itemView.findViewById(R.id.tvWords);
        }
    }

    public static interface Listener {
        void onWordClick(String word);
    }

}
